<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaDeptGradeController extends OaBaseController{
    var $model;
    function _initialize() {
        $this->model = $this->getModel ( 'oa_dept_grade' );
        parent::_initialize ();
        $controller = strtolower ( CONTROLLER_NAME );

        $res ['title'] = '部门信息';
        $res ['url'] = addons_url ( 'OaSystem://OaDept/lists' ,array('mdm'=>I('mdm')));
        $res ['class'] = $controller == 'oadept' ? 'cur' : '';
        $nav [] = $res;
        $this->assign ( 'nav', $nav );

        $res ['title'] = '部门列表';
        $sub_nav[] = $res;
        $res ['title'] = '部门等级';
        $res ['url'] = addons_url ( 'OaSystem://OaDeptGrade/lists' ,array('mdm'=>I('mdm')));
        $res ['class'] = $controller == 'oadeptgrade' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '部门关系';
        $res ['url'] = addons_url ( 'OaSystem://OaDept/show' ,array('mdm'=>I('mdm')));
        $res ['class'] = $controller == 'oadept' ? 'cur' : '';
        $sub_nav[] = $res;
        $this->assign ( 'sub_nav', $sub_nav );
    }

    function lists(){
        $list_data = $this->_get_model_list( $this->model );
        $this -> assign($list_data);
        $this->display();
    }

    function add(){
        if(IS_POST) {
            $data['appid'] = $this->appId;
            $data['grade_no'] = $_POST['grade_no'];
            $data['name'] = $_POST['name'];
            $data['remark'] = $_POST['remark'];

            if(db_find('oa_dept_grade', ['name'=>$data['name']])) {
                $this->error('该等级名称已经存在, 请重新输入！');
                return false;
            }

            if(db_find('oa_dept_grade', ['grade_no'=>$data['grade_no']])) {
                $this->error('该等级编号已经存在， 请重新输入！');
                return false;
            }

            M('oa_dept_grade')->add($data);

            $this->success ( '创建' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
        } else {
            $this->display('add');
        }
    }
}